<template>
  <div class="slider" ref="slider">
    <div class="slider-group" ref="sliderGroup">
      <slot></slot>
    </div>

    <div class="dots">
      <span
        v-for="(item, index) of dots"
        :key="index"
        class="dot"
        :class="{ active: index === currentPageIndex }"
      ></span>
    </div>
  </div>
</template>

<script>

import { addClass } from 'assets/js/dom'
import BScroll from 'better-scroll'

export default {
  name: 'slider',
  props: {
    loop: {
      type: Boolean,
      default: true
    },
    autoPlay: {
      type: Boolean,
      default: true
    },
    interval: {
      type: Number,
      default: 4000
    }
  },
  data() {
    return {
      currentPageIndex: 0,
      dots: []
    }
  },
  mounted() {

    setTimeout(() => {
      this._setSliderWidth()
      this._initDols()
      this._initSlider()


      // 手动设置轮播效果
      // if (this.autoPlay) {
      //   this._play()
      // }

      window.addEventListener('resize', () => {
        this.refresh()
      })
    }, 20)
  },
  activated() {

    // slider没初始化成功再来一次
    if (this.children[0].clientWidth <= 0) {
      this._setSliderWidth()
      this._initSlider()
    }

    this.refresh()
    this.slider.enable()
  },
  deactivated() {
    this.slider.disable()
  },
  methods: {
    _setSliderWidth() {
      this.children = this.$refs.sliderGroup.children
      let sliderWidth = this.$refs.slider.clientWidth
      this.children.forEach(item => {
        addClass(item, 'slider-item')
        item.style.width = sliderWidth + 'px'
      })
    },
    _initDols() {
      this.dots = Array.from({ length: this.children.length })
    },
    _initSlider() {
      if (this.children[0].clientWidth <= 0) {
        return
      }

      this.slider = new BScroll(this.$refs.slider, {
        scrollX: true,
        scrollY: false,
        momentum: false,
        bounce: false,
        click: true, //移动端派发click事件，否则无法点击
        slide: {
          loop: this.loop,
          threshold: 0.3,
          // 切换下一个或上一个 Page 的阈值,当滚动距离小于该阈值时，不会触发切换到下一个或上一个。
          speed: 400,
          interval: this.interval,
          autoplay: this.autoPlay
        }
      })

      this.slider.on('scrollEnd', this._onSrollEnd)

      // 手动设置轮播效果
      // this.slider.on('touchEnd', () => {
      //   if (this.autoPlay) {
      //     this._play()
      //   }
      // })

      // this.slider.on('beforeScrollStart', () => {
      //   if (this.autoPlay) {
      //     clearInterval(this.timer)
      //   }
      // }) // beforeScrollStart 触发时机：用户手指放在滚动区域的时候
    },
    // 手动设置轮播效果
    // _play() {
    //   if (!this.autoPlay) {
    //     return
    //   }

    //   clearInterval(this.timer)
    //   this.timer = setInterval(() => {
    //     this.slider.next()
    //   }, this.interval)
    // },
    _onSrollEnd() {
      let pageIndex = this.slider.getCurrentPage().pageX
      this.currentPageIndex = pageIndex
    },
    refresh() {
      if (this.slider) {
        this._setSliderWidth()
        this.slider.refresh()
      }
    }
  }
}
</script>

 <style lang="stylus" scoped>
 @import '~assets/stylus/variable';

 .slider {
   position: relative;
   width: 100%;
   height: 100%;

   .slider-group {
     position: relative;
     display: inline-block;
     white-space: nowrap;

     .slider-item {
       display: inline-block;
       text-align: center;

       a {
         display: block;
         width: 100%;
         text-decoration: none;

         img {
           display: block;
           width: 100%;
         }
       }
     }
   }

   .dots {
     position: absolute;
     right: 0;
     left: 0;
     bottom: 12px;
     transform: translateZ(1px);
     text-align: center;
     font-size: 0;

     .dot {
       display: inline-block;
       margin: 0 4px;
       width: 8px;
       height: 8px;
       border-radius: 50%;
       background: $color-text-l;

       &.active {
         width: 20px;
         border-radius: 5px;
         background: $color-text-ll;
       }
     }
   }
 }
</style> 